package com.hhkj.api.sale.controller;

import com.hhkj.admin.order.model.req.OrderItemReq;
import com.hhkj.admin.sale.common.component.supplier.SaleSupplier;
import com.hhkj.admin.train.model.dopo.TrainDo;
import com.hhkj.admin.train.service.base.TrainService;
import com.hhkj.api.sale.common.component.manager.ApiSaleLoginManager;
import com.hhkj.api.sale.common.session.holder.ApiSaleSessionHolder;
import com.hhkj.api.sale.common.session.model.LoginSale;
import com.hhkj.api.sale.common.valid.ApiSaleRegister;
import com.hhkj.api.sale.model.req.*;
import com.hhkj.api.sale.service.biz.ApiSaleBizService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.login.api.ApiLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * ApiSaleController
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/10/25 17:40
 */
@RequestMapping("/api/sale")
@RestController
public class ApiSaleController extends BaseController {

    @Autowired
    private ApiSaleBizService apiSaleBizService;
    @Autowired
    private ApiSaleLoginManager loginManager;
    @Autowired
    private SaleSupplier saleSupplier;

    /** 重置密码 */
    @ApiLogin(LoginSale.class)
    @PutMapping("/password")
    public AjaxResult updatePassword(@Validated @RequestBody ApiSaleUpdatePwReq req) {
        return apiSaleBizService.updatePassword(req) ? AjaxResult.success() : AjaxResult.error();
    }

    /** 重置密码 */
    @PutMapping("/password/reset")
    public AjaxResult resetPassword(@Validated @RequestBody ApiSaleResetPasswordReq req) {
        return apiSaleBizService.resetPassword(req) ? AjaxResult.success() : AjaxResult.error();
    }

    /** 邀请用户分页信息 */
    @ApiLogin(LoginSale.class)
    @PostMapping("/children/paging")
    public TableDataInfo pagingChildren(@RequestBody ApiSaleReq req) {
        return this.getDataTable(apiSaleBizService.pagingChildren(req));
    }

    /** 获取邀请码 */
    @ApiLogin(LoginSale.class)
    @GetMapping("/invitation/code/url")
    public AjaxResult getInvitationCodeUrl() {
        return AjaxResult.success(saleSupplier.getInvitationCodeUrlById(ApiSaleSessionHolder.getSaleId()));
    }

    /** 获取当前用户信息 */
    @ApiLogin(LoginSale.class)
    @GetMapping()
    public AjaxResult get() {
        return AjaxResult.success(ApiSaleSessionHolder.getLoginSale());
    }

    /** 退出 */
    @ApiLogin(LoginSale.class)
    @PostMapping("/logout")
    public AjaxResult logout() {
        return loginManager.doLoginOut() ? AjaxResult.success() : AjaxResult.error();
    }

    /** 登录 */
    @PostMapping("/login")
    public AjaxResult login(@Validated @RequestBody ApiSaleLoginReq req) {
        return AjaxResult.success(loginManager.doLogin(req));
    }

    /** 注册 */
    @PostMapping("/register")
    public AjaxResult register(@Validated(ApiSaleRegister.class) @RequestBody ApiSaleRegisterReq req) {
        return apiSaleBizService.register(req) ? AjaxResult.success() : AjaxResult.error();
    }
}
